# encoding: utf-8
# @author: wangxin
# @software: garner
# @file: OrderSettingList.py
# @time: 2025/1/24 11:31
from django.core.paginator import Paginator
from django.http import HttpResponse
from calendar import month
from turtledemo.penrose import start

import pymysql
from django.db.models.expressions import result
from django.views import View
import json
from tabnanny import check

# 导入Django核心模块中的序列化器，用于将Django模型转换为其他格式，如JSON、XML等
from django.core import serializers

# 导入Django序列化功能，以便在需要时将模型序列化
from django.core.serializers import serialize
from django.db.models import Q

# 导入HttpResponse和JsonResponse类，用于构建HTTP响应和JSON响应
from django.http import HttpResponse, JsonResponse

# 导入render函数，它用于根据请求和模板渲染网页
from django.shortcuts import render
import time

from django.views import View

from backend.Maxday import get_max_days_in_month

class OrderSettingListView(View):
    def get(self,request,Page,PageSize):
        queryString = request.GET.get("queryString")
        print(queryString)
        if (queryString == None):
            conn = pymysql.Connect(
                host="127.0.0.1",
                port=3306, user="root",
                password="123456",
                db="ruici",
                charset="utf8",
                autocommit=True
            )
            cursor = conn.cursor()
            sql = f"SELECT  o.orderDate,o.orderType,o.orderStatus,m.name as username,s.`name` FROM t_order AS o,t_member AS m,t_setmeal AS s WHERE o.member_id=m.id  AND  o.setmeal_id=s.id LIMIT 0,{PageSize}"
            cursor.execute(sql)
            res = cursor.fetchall()
            #统计t_order中订单的总数
            sql2 = f"SELECT COUNT(*) FROM t_order"
            cursor.execute(sql2)
            total_count=cursor.fetchone()[0]


            print(res)
            data_list = list()
            for data in res:
                data_dict=dict()
                data_dict["oederDate"] = data[0]
                data_dict["orderType"] = data[1]
                data_dict["oederStatus"] = data[2]
                data_dict["username"] = data[3]
                data_dict["projectname"] = data[4]

                data_list.append(data_dict)
            result={
                "data":data_list,
                "total_count": total_count,
                "status":200
            }



            return JsonResponse(result)
        else:
            conn = pymysql.Connect(
                host="127.0.0.1",
                port=3306, user="root",
                password="123456",
                db="ruici",
                charset="utf8",
                autocommit=True
            )
            cursor = conn.cursor()
            sql = f"SELECT o.orderDate,o.orderType,o.orderStatus,m.name as username,s.`name` FROM t_order AS o,t_member AS m,t_setmeal AS s WHERE o.member_id=m.id  AND  o.setmeal_id=s.id AND m.name LIKE '%{queryString}%' LIMIT 0,{PageSize}"
            cursor.execute(sql)
            res = cursor.fetchall()
            # 统计t_order中订单的总数
            sql2 = f"SELECT COUNT(*) FROM t_order"
            cursor.execute(sql2)
            total_count = cursor.fetchone()[0]
            data_list = list()
            for data in res:
                data_dict = dict()
                data_dict["oederDate"] = data[0]
                data_dict["orderType"] = data[1]
                data_dict["oederStatus"] = data[2]
                data_dict["username"] = data[3]
                data_dict["projectname"] = data[4]
                data_list.append(data_dict)
                result={
                    "data":data_list,
                    "status":200
                }
                return JsonResponse(result)